01. Writing Structs with Properties

Writing Structs with Properties Intro

Why Structs?

We highly recommend that you follow along with this lesson by opening the Writing Structs with Properties.playground file in Playground Collection → Part 1 - Pirate Fleet → Writing Structs with Properties of the Beginning iOS Playground Collection. When you open the playground, visit the Editor menu, scroll to the bottom, and click "Show Rendered Markup".

There are many instances in programming where you need more than one variable to represent an object. For example, to represent a student, you may require values for name, age, and school. A rudimentary approach to representing a student might be to create separate variables for each value. Here are examples of doing that in Swift:

var name = "Ayush Saraswat"
var age = 19
var school = "Udacity"

However, we now have three related variables that aren’t grouped together in any way. For each additional student, we would have to create a new set of these three variables, quickly resulting in complicated and incredibly confusing code.

var name1 = "Ayush Saraswat"
var age1 = 19
var school1 = "Udacity"

var name2 = "Jarrod Parkes"
var age2 = 24
var school2 = "Udacity"

Fortunately, languages like Swift offer a solution to this problem in the form of structs. Structs allow us to package multiple variables into a single type.

Defining a Struct

To create a struct in Swift, we can use the following syntax:

struct NameOfStruct {
    // Variables
}

Student Example

When you are choosing a name for a struct, always start the name with a capital letter. This avoids any confusion between basic variable data types and structs. You should also strive to keep the names descriptive while being as concise as possible. For example, for a struct representing students, we can create a struct called Student:

struct Student {
    // Variables
}

The variables inside of a struct follow the same formula for declaration we saw in the variables skill. That is, start with let or var depending on if you want a constant or a variable.

In this example, we'll use let for the name since we don’t want the student’s name to change in our application. We also don’t want to assign an initial value here because each student will have their own name. So we will add a colon followed by the explicit type, which is String.

struct Student {
    let name: String
}

Then, we will continue this for the student's age and school. When done, we have a struct that encapsulates all the variables we want into one nice package. Notice that age and school are declared as var, or variables, because these are values that can change.

struct Student {
    let name: String
    var age: Int
    var school: String
}

Defining a Struct Means Defining a Type

When we define the Student struct, we've created a new data type. So Student is a data type just like Integer is a type, just like String is a type, and so on. The only difference is a Student is not a basic data type and it contains multiple values.

Geolocation Example

Let’s see this syntax again with a GeoLocation struct:

struct GeoLocation {
    var latitude: Double = 0.0
    var longitude: Double = 0.0
}

This GeoLocation struct contains two variables, also known as members or properties, both of which are of type Double. (Property is the term used by Apple and we will use it as much as possible—be sure to make note of it!)

You may also notice that both properties are initialized to 0.0. It is not required that we initialize properties, but setting the initial values is nice because it gives us default values to work with right away.

The last thing to note is that each property is declared with var, meaning that they are mutable or can be changed.

Game Example

And, let’s look at one final struct that is common in game applications.

struct Point2D {
    var x: Int = 0
    var y: Int = 0
}

This struct has properties for the x and y coordinates in a 2D space. We could have just called it Point, but Point2D is a better name because it disambiguates this struct from a 3D point that contains a z-coordinate.

Initialization Vars In Struct Declaration

In the GeoLocation example we talked a little bit about initializing default property values of a struct.

struct GeoLocation {
    var latitude: Double = 0.0
    var longitude: Double = 0.0
}

For the GeoLocation struct, the default initialization all takes place here, where we define the struct. However, this is not the only way to initialize property values. And remember, this is just the definition of a struct—we haven't actually created anything yet.